package com.citywy.view;

import com.citywy.entity.News;
import com.citywy.service.NewsService;
import com.citywy.util.PageUtil;
import com.citywy.util.PublicUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * /news.htm
 */
public class NewsController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        NewsService newsService=new NewsService();
        //获取参数
        int pageNow= PublicUtil.getParam(request,"pn",1);
        int pageSize=PublicUtil.getParam(request,"x_size",6);
        String x_order=PublicUtil.getParam(request,"x_order","id");//根据什么字段排序
        String x_sort=PublicUtil.getParam(request,"x_sort","desc");
        String x_name=PublicUtil.getParam(request,"x_name",null);
        System.out.println("输出pn"+pageNow);
        //查询条件
        String condition = null;
        if(x_name!=null && !"".equals(x_name)){
            condition = "title like '%"+x_name+"%'";
        }

        int rowcount=newsService.getRowCount(condition);
        int pageCount=(rowcount-1)/pageSize+1;

        List<News> list=newsService.getNewsList(condition,pageNow,pageSize,x_order,x_sort);

        //分页参数
        PageUtil pageUtil = new PageUtil(pageNow,pageSize,pageCount,rowcount,x_order,x_sort,x_name);

        request.setAttribute("list", list);
        request.setAttribute("pageutil",pageUtil);

        request.getRequestDispatcher("/pages/news.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
